package cn.zswltech.gruul.web.api.controller;

import cn.hutool.core.util.StrUtil;
import cn.zswltech.gruul.api.OrgApi;
import cn.zswltech.gruul.biz.service.OrgService;
import cn.zswltech.gruul.common.result.Response;
import cn.zswltech.gruul.common.util.StringUtil;
import cn.zswltech.gruul.common.entity.DisplayGroupDO;
import cn.zswltech.gruul.common.entity.OrgDO;
import cn.zswltech.gruul.web.api.controller.common.BaseController;
import cn.zswltech.gruul.web.api.util.CookieUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
public class OrgController extends BaseController implements OrgApi {

    @Resource(name = "orgService")
    private OrgService orgService;

    /**
     * 获取机构关联的菜单和功能
     *
     * @return
     * @
     */
    @Override
    @GetMapping("menu")
    public Response<List<DisplayGroupDO>> getMenuFunctions(Long orgId) {
        return orgService.getMenuFunctions(orgId);
    }

    /**
     * 机构关联菜单和功能
     *
     * @return
     * @
     */
    @Override
    @PostMapping("menu")
    public Response setMenuFunctions() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Long orgId = Long.valueOf(request.getParameter("orgId"));
        String params = request.getParameter("params");
        return orgService.setMenuFunctions(orgId, params);
    }

    /**
     * 获取全部机构
     *
     * @param orgOnly 是否仅选择机构
     *
     * @param orgSelect 是否只返回用户所属的机构
     * @return
     *
     */
    @Override
    @GetMapping
    public Response<List<OrgDO>> getOrgList(Boolean orgOnly,Boolean orgSelect) {
        return orgService.getOrgList(orgOnly,orgSelect);
    }

    /**
     * 创建机构
     *
     * @return
     * @
     */
    @Override
    @PostMapping
    public Response createOrg() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        OrgDO orgDO = genOrgDO(request);
        orgDO.setLevel(StringUtil.str2Integer(request.getParameter("level")));
        if (StringUtil.isNotEmpty(request.getParameter("parentId"))) {
            orgDO.setParentId(Long.valueOf(request.getParameter("parentId")));
        }
        Date now = new Date();
        orgDO.setGmtCreate(now);
        orgDO.setGmtModified(now);
        return orgService.createOrg(orgDO);
    }

    /**
     * 更新机构
     *
     * @return
     * @
     */
    @Override
    @PutMapping
    public Response updateOrg() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        OrgDO orgDO = genOrgDO(request);
        orgDO.setId(Long.valueOf(request.getParameter("id")));
        return orgService.updateOrg(orgDO);
    }

    /**
     * 删除机构
     *
     *
     * @param id
     * @return
     * @
     */
    @Override
    @DeleteMapping
    public Response deleteOrg(@Param("id") Long id) {
        return orgService.deleteOrg(id);
    }

    private OrgDO genOrgDO(HttpServletRequest request) {
        OrgDO orgDO = new OrgDO();
        orgDO.setSystemCode(request.getHeader(CookieUtils.SYSTEM_CODE));
        orgDO.setCode(request.getParameter("code"));
        orgDO.setType(StrUtil.isNotEmpty(request.getParameter("type"))?Integer.valueOf(request.getParameter("type")): null);
        orgDO.setName(request.getParameter("name"));
        orgDO.setEnName(request.getParameter("enName"));
        return orgDO;
    }
}
